package com.xinkao.log.entity;

import java.io.Serializable;
import java.util.List;

/**
 * @author 周洋
 * @Description
 * @createTime 2021/9/27 14:18
 */
public class Page<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总数
     */
    protected long total = 0;
    /**
     * 每页显示条数，默认 10
     */
    protected long size = 10;

    /**
     * 当前页
     */
    protected long current = 1;

    /**
     * 总页数
     */
    protected long pages = 1;

    /**
     * 结果列表
     */
    private List<T> rows;

    public Page(){
        this.current = 1;
        this.size = 10;
    }
    public Page(int currentPage, int pageSize){
        this.current=currentPage<=0?1:currentPage;
        this.size=pageSize<=0?1:pageSize;
    }
    public long getSize() {
        return size;
    }
    public void setSize(long pageSize) {
        this.size = pageSize;
    }
    public long getCurrent() {
        return this.current;
    }

    public Page<T> setCurrent(long current) {
        this.current = current;
        return this;
    }
    public long getTotal() {
        return this.total;
    }

    public Page<T> setTotal(long total) {
        this.total = total;
        return this;
    }
    public void setPages(long pages){
        this.pages = pages;
    }
    public long getPages(){
        return this.pages;
    }

    /**
     * 设置结果 及总页数
     * @param rows
     */
    public void build(List<T> rows) {
        this.setRows(rows);
        long count =  this.getTotal();
        long divisor = count / this.getSize();
        long remainder = count % this.getSize();
        this.setPages(remainder == 0 ? divisor == 0 ? 1 : divisor : divisor + 1);
    }
    public List<T> getRows() {
        return rows;
    }
    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}